package com.code.manager;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 从XT交易所中获取实时兑换比例
 *
 * @author tangyifei
 * @since 2020年1月6日18:37:55
 */
@Component
@Slf4j
public class ExchangeHouseManager {

    @Value("${XT.single.twentyFourHoursMarket.url}")
    private String url;

    @Autowired
    private RestTemplate restTemplate;

//    @Resource
//    private ProfileConfig profileConfig;

    /**
     * 获取兑换比例
     *
     * @param coinExchange 币种兑换 XWC_USDT，BTC_USDT
     * @return 兑换比例
     */
    public String getTwentyFourHoursMarket(String coinExchange) {
        ResponseEntity<XTResponse> entity = restTemplate.getForEntity(url + "?marketName=" + coinExchange, XTResponse.class);
//        String profile = profileConfig.getActiveProfile();
//        log.info("当前的环境：【{}】", profile);
//        if (ServerConstants.QA.equals(profile)) {
//            log.info("获取某一个币种实时单个市场24H市场行情：【{}】", entity);
//        }
        HttpStatus httpStatus = entity.getStatusCode();
        String exchangeRatio = null;
        if (HttpStatus.OK.value() == httpStatus.value()) {
            XTResponse xtResponse = entity.getBody();
            String[] str = xtResponse.getDatas();
            if (ArrayUtils.isNotEmpty(str) && str.length >= 2) {
                exchangeRatio = str[1];
            }
//            log.info("获取XT交易所{}的实时的币种兑换比例：{}", coinExchange, exchangeRatio);
        }
        return exchangeRatio;
    }

    /**
     * xt响应类
     *
     * @author tangyifei
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class XTResponse {

        private String[] datas;

        private Object resMsg;

    }


}
